#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDebug>
#include <QFile>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
//    QFile files("D:\\Qt\\Documents\\DataStructureCourseDesign\\all.txt");
//    files.open(QIODevice::ReadOnly);
//    while(!files.atEnd()){
//        QByteArray line = files.readLine();
//        QString str(line);
//        string s = str.toStdString();
//        int len = s.size();
//        string s1 = "",s2 = "";
//        int i;
//        for(i = 0; i < len && s[i] != ' '; ++i){
//            if(str[i] != ' '){
//                s1 += s[i];
//            }
//        }
//        for(i++;i < len; ++i){
//            s2 += s[i];
//        }
//        pair<string,string> p = make_pair(s1,s2);
//        AVL->insertNode(p);
//    }
    //
    AVL->load("D:\\Qt\\Documents\\DataStructureCourseDesign\\all.txt");
    ui->setupUi(this);
    this->setWindowTitle("英汉字典");
    ui->stackedWidget->setCurrentIndex(0);

    connect(ui->actionSign,&QAction::triggered,this,[=](){
        ui->stackedWidget->setCurrentIndex(1);
    });

    connect(ui->actionExit,&QAction::triggered,this,[=](){
        this->close();
    });
    //登陆权限
    connect(ui->page1_signButton,&QPushButton::clicked,this,[=](){
        QString userName = ui->page1_Text1->text();
        QString password = ui->page1_Text2->text();
        if(userName == "fangyuhao" && password == "fyh163617"){
            ui->stackedWidget->setCurrentIndex(2);
        }else{
            QMessageBox::critical(this,"criticaL","用户名或密码错误");
        }
    });
    //清空
    connect(ui->pushButton_2,&QPushButton::clicked,this,[=](){
        ui->textEdit_2->setText(tr(""));
        ui->textEdit->setText(tr(""));
    });

    connect(ui->pushButton_5,&QPushButton::clicked,this,[=](){
        ui->textEdit_3->setText(tr(""));
        ui->textEdit_4->setText(tr(""));
    });
    
    connect(ui->page1_backButton,&QPushButton::clicked,[=](){
        ui->stackedWidget->setCurrentIndex(0);
    });
    
    //查找单词
    connect(ui->pushButton,&QPushButton::clicked,[=](){
        QString s = ui->textEdit_2->toPlainText();
        string value = AVL->search(s.toStdString());
        if(value.size() != 0){
            ui->textEdit->setText(QString::fromStdString(value));
        }else{
            QMessageBox::information(this,"查找单词","未找到该单词！");
        }
    });

    connect(ui->pushButton_4,&QPushButton::clicked,[=](){
        QString s = ui->textEdit_3->toPlainText();
        string value = AVL->search(s.toStdString());
        if(value.size() != 0){
            ui->textEdit_4->setText(QString::fromStdString(value));
        }else{
            QMessageBox::information(this,"查找单词","未找到该单词！");
        }
    });

    //插入单词
    connect(ui->pushButton_6,&QPushButton::clicked,[=](){
        QString s1 = ui->textEdit_3->toPlainText();
        QString s2 = ui->textEdit_4->toPlainText();
        if(s1.size() == 0|| s2.size() == 0){
            QMessageBox::information(this,"插入单词","插入单词错误！");
        }
        pair<string,string>p = make_pair(s1.toStdString(),s2.toStdString());
        if(AVL->insertNode(p)){
            QMessageBox::information(this,"插入单词","新建单词成功！");
        }else{
            QMessageBox::information(this,"插入单词","插入重复单词！");
        }
    });

    //删除单词
    connect(ui->pushButton_7,&QPushButton::clicked,[=](){
        QString s = ui->textEdit_3->toPlainText();
        if(s.size() == 0){
            QMessageBox::information(this,"插入单词","删除单词错误！");
        }
        if(AVL->deleteNode(s.toStdString())){
            QMessageBox::information(this,"删除单词","成功删除！");
        }else{
            QMessageBox::information(this,"删除单词","删除失败，未找到该单词！");
        }
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}
